import 'package:flutter/material.dart';
import 'package:timer_builder/timer_builder.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';

Dio dio = new Dio();

class Jifen extends StatefulWidget {
  Jifen({Key key}) : super(key: key);

  _JifenState createState() => _JifenState();
}

class _JifenState extends State<Jifen> {
  var user_id;
  var number;
  var login; //登录
  var yaowen; //阅读要闻
  var study; //阅读学习
  var studyTime; //  文章学习时长
  var dayli; //  每日答题
  var weekend; //  每周答题
  var viewpoint; //  发表观点
  var leijijifen = 0; //  累计积分
  var sumjifen; //  总积分
  getName() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    user_id = sharedPreferences.getString('user_id');
    number = sharedPreferences.getString('number');
    getrenwu();
    setState(() {});
  }

  Future getrenwu() async {
    Response response;
    if (number.length < 10) {
      response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/teacher/${user_id}'); //id 传进来的 //student/id  teacher/id
    } else {
      response = await dio.get(
          'http://xssw.gdit.edu.cn/sizheng/api/v1/student/${user_id}'); //id 传进来的 //student/id  teacher/id
    }
    sumjifen = response.data['point'];
    login = response.data['mission']['login'];
    yaowen = response.data['mission']['yaowen'];
    study = response.data['mission']['study'];
    studyTime = response.data['mission']['studyTime'];
    dayli = response.data['mission']['dayli'];
    weekend = response.data['mission']['weekend'];
    viewpoint = response.data['mission']['viewpoint'];
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getName();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('学习积分'),
        centerTitle: true,
        // actions: <Widget>[
        //   InkWell(
        //     child: Container(
        //       margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
        //       child: Text('积分明细', style: TextStyle(fontSize: 15.0)),
        //       alignment: Alignment.center,
        //     ),
        //     onTap: () {},
        //   )
        // ],
      ),
      body: ListView(
        children: <Widget>[
          Stack(
            alignment: Alignment.topCenter,
            children: <Widget>[
              Image.asset('images/bg3.jpg'),
              Container(
                margin: EdgeInsets.fromLTRB(0, 50, 0, 0),
                child: Text(
                  '${sumjifen}',
                  style: TextStyle(fontSize: 42),
                ),
              ),
              Container(
                  width: 60,
                  height: 30,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(width: 1, color: Colors.white),
                    borderRadius: BorderRadius.all(Radius.circular(8)),
                  ),
                  margin: EdgeInsets.fromLTRB(0, 120, 0, 0),
                  child: InkWell(
                    child: Text(
                      '去兑换',
                      style: TextStyle(
                          fontSize: 12, color: Color.fromRGBO(227, 60, 100, 1)),
                    ),
                    onTap: () {
                      print('object8');
                    },
                  )),
              Container(
                  width: 310,
                  height: 30,
                  margin: EdgeInsets.fromLTRB(0, 185, 0, 0),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    border: Border.all(
                        width: 1, color: Color.fromRGBO(220, 220, 220, 1)),
                    borderRadius: BorderRadius.all(Radius.circular(9)),
                    boxShadow: [
                      BoxShadow(
                          color: Color.fromRGBO(220, 220, 220, 1),
                          offset: Offset(2.0, 2.0),
                          blurRadius: 3.0,
                          spreadRadius: 1.0), 
                    ],
                  ),
                  child: Row(
                    children: <Widget>[
                      SizedBox(
                        width: 5,
                      ),
                      Image.asset(
                        'images/broadcast.png',
                        width: 20,
                        height: 20,
                      ),
                      SizedBox(
                        width: 5,
                      ),
                      Container(
                        width: 275,
                        child: ClockWidget(),
                      )
                    ],
                  ))
            ],
          ),
          Container(
            margin: EdgeInsets.fromLTRB(10, 25, 10, 0),
            child: Column(
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                      child: Row(
                        children: <Widget>[
                          Container(
                            width: 3,
                            height: 18,
                            color: Colors.red,
                          ),
                          SizedBox(
                            width: 3,
                          ),
                          Text(
                            '积分规则',
                            style: TextStyle(fontWeight: FontWeight.bold),
                          ),
                        ],
                      ),
                    ),
                    Container(
                      child: Row(
                        children: <Widget>[
                          Text('今日任务'),
                        ],
                      ),
                    )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '登录',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '1分/每日首次登陆',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        login != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    login != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '阅读要闻',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '1分/每有效阅读一篇要闻',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        yaowen != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    yaowen != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '阅读学习',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '1分/每有效阅读一篇学习文章',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        study != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    study != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                    // Container(
                    //   width: 60,
                    //   height: 25,
                    //   alignment: Alignment.center,
                    //   decoration: BoxDecoration(
                    //       color: Color.fromRGBO(227, 60, 100, 1),
                    //       border: Border.all(width: 1, color: Colors.white),
                    //       borderRadius: BorderRadius.all(Radius.circular(3))),
                    //   child: InkWell(
                    //     child: Text(
                    //       '去看看',
                    //       style: TextStyle(fontSize: 12, color: Colors.white),
                    //     ),
                    //     onTap: () {
                    //       Navigator.pop(context);
                    //     },
                    //   ),
                    // )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '文章学习时长',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '1分/有效阅读文章累计2分钟',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        studyTime != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    studyTime != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '每日答题',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '每组答题答对2-4道积1分，答对5题积2分',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        dayli != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    dayli != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '每周答题',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '每组答题答对1道积1分，同组答题不重复积分',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        weekend != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    weekend != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Container(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
                          child: Text(
                            '发表观点',
                            style: TextStyle(fontSize: 18),
                          ),
                        ),
                        Text(
                          '1分/每发布一个有效观点',
                          style: TextStyle(
                              fontSize: 13,
                              color: Color.fromRGBO(107, 107, 107, 1)),
                        ),
                        viewpoint != 'true'
                            ? Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color: Color.fromRGBO(
                                              200, 200, 200, 200),
                                          border: Border.all(
                                            width: 1,
                                            color: Color.fromRGBO(
                                                200, 200, 200, 200),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                            : Container(
                                margin: EdgeInsets.fromLTRB(0, 8, 0, 4),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 75,
                                      height: 5,
                                      margin: EdgeInsets.fromLTRB(0, 0, 5, 0),
                                      decoration: BoxDecoration(
                                          color:
                                              Color.fromRGBO(227, 60, 100, 1),
                                          border: Border.all(
                                            width: 1,
                                            color:
                                                Color.fromRGBO(227, 60, 100, 1),
                                          ),
                                          borderRadius: BorderRadius.all(
                                              Radius.circular(4))),
                                    ),
                                  ],
                                ),
                              )
                      ],
                    )),
                    Divider(),
                    viewpoint != 'true'
                        ? Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/incomplete.png'),
                          )
                        : Container(
                            width: 50,
                            height: 50,
                            child: Image.asset('images/completed.png'),
                          )
                  ],
                ),
                Divider(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class ClockWidget extends StatelessWidget {
  var _text = '积分试用阶段,积分规则会实时调整,带来不便请谅解。';
  var first;
  var last;
  String clock() {
    first = _text.substring(0, 1);
    last = _text.substring(1);
    _text = last.toString() + first.toString();
    return _text;
  }

  @override
  Widget build(BuildContext context) {
    return TimerBuilder.periodic(Duration(seconds: 1), builder: (context) {
      return Text("${clock()}",
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            color: Color.fromRGBO(193, 193, 193, 1),
            fontSize: 12,
          ));
    });
  }
}
